/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.ui;

import org.chaoticengine.cgll.exceptions.LoadingException;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public abstract class TextWidget extends Widget {
    protected String value = "";
    protected String renderedValue = "";
    protected Image img = null;
    protected Graphics imgGraphics = null;
    protected UnicodeFont font = null;

    public TextWidget(int charWidth) throws LoadingException {
        // Load the font
        try {
            font = new UnicodeFont("data/fonts/SF Digital Readout Medium.ttf", 32, false, false);
            font.getEffects().add(new ColorEffect(java.awt.Color.white));
            font.addAsciiGlyphs();
            font.loadGlyphs();
        } catch (SlickException sEx) {
            throw new LoadingException(sEx);
        }

        String widthStr = "";
        for (int i = 0; i < charWidth; i++) {
            widthStr += "9";
        }

        // Create the widget image buffer
        try {
            img = new Image(font.getWidth(widthStr), font.getHeight(widthStr));
            imgGraphics = img.getGraphics();
            imgGraphics.setFont(font);
        } catch (SlickException sEx) {
            throw new LoadingException(sEx);
        }

        updateImage();
    }

    private void updateImage() {
        if (!value.equals(renderedValue)) {
            renderedValue = value;
            int w = font.getWidth(renderedValue);
            int h = font.getHeight(renderedValue);

            imgGraphics.clear();
            imgGraphics.drawString(renderedValue, 0, 0);
            imgGraphics.flush();
        }
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public Rectangle getBounds() {
        if (img != null) {
            return(new Rectangle(0.0f, 0.0f, img.getWidth(), img.getHeight()));
        } else {
            return(new Rectangle(0.0f, 0.0f, 0.0f, 0.0f));
        }

    }

    @Override
    public void render(Vector2f position, GameContainer gc, StateBasedGame sb, Graphics gr) {
        if (img != null) {
            gr.drawImage(img, position.x, position.y);
        }
    }

    @Override
    public void update() {
        updateImage();
    }
}
